Here, we will apply a k-nearest neighbor (KNN) algorithm to classify the scATAC cells to a given cell type category with the help of our training set, the Multiome experiment. Remember, that KNN works on a basic assumption that data points of similar categories are closer to each other.
library(Seurat)
library(Signac)
library(flexclust)
library(tidyverse)
library(plyr)
library(harmony)
library(class)
library(ggplot2)
library(reshape2)
library(ggpubr)
set.seed(222)
cell_type = "CD4_T"
# Paths
path_to_obj <- str_c(
here::here("scATAC-seq/results/R_objects/level_5/"),
cell_type,
"/01.",
cell_type,
"_integrated_level_5.rds",
sep = ""
)
path_to_obj_RNA <- str_c(
here::here("scRNA-seq/3-clustering/5-level_5/"),
cell_type,
"/",
cell_type,
"_subseted_annotated_level_5.rds",
sep = ""
)
path_to_level_4 <- here::here("scATAC-seq/results/R_objects/level_5/CD4_T/")
path_to_save <- str_c(path_to_level_4, "02.CD4_T_annotated_level_5.rds")
reduction <- "harmony"
dims <- 1:40
color_palette <- c("#1CFFCE", "#90AD1C", "#C075A6", "#85660D",
"#5A5156", "#AA0DFE", "#F8A19F", "#F7E1A0",
"#1C8356", "#FEAF16", "#822E1C", "#C4451C",
"#1CBE4F", "#325A9B", "#F6222E", "#FE00FA",
"#FBE426", "#16FF32", "black", "#3283FE",
"#B00068", "#DEA0FD", "#B10DA1", "#E4E1E3",
"#90AD1C", "#FE00FA", "#85660D", "#3B00FB",
"#822E1C", "coral2", "#1CFFCE", "#1CBE4F",
"#3283FE", "#FBE426", "#F7E1A0", "#325A9B",
"#2ED9FF", "#B5EFB5", "#5A5156", "#DEA0FD",
"#FEAF16", "#683B79", "#B10DA1", "#1C7F93",
"#F8A19F", "dark orange", "#FEAF16",
"#FBE426", "Brown")
We need to load the scRNAseq annotation from Multiome experiment (cell barcode and cell-type assigned) and the integrated scATAC data.
seurat <- readRDS(path_to_obj_RNA)
tonsil_RNA_annotation <- seurat@meta.data %>%
rownames_to_column(var = "cell_barcode") %>%
dplyr::filter(assay == "multiome") %>%
dplyr::select("cell_barcode", "annotation_paper")
head(tonsil_RNA_annotation)
## cell_barcode annotation_paper
## 1 co7dzuup_xuczw9vc_AAACATGCAAGCCAGA-1 GC-Tfh-0X40
## 2 co7dzuup_xuczw9vc_AAACATGCAAGGTATA-1 Naive
## 3 co7dzuup_xuczw9vc_AAACCGGCATGCTATG-1 Tfh-LZ-GC
## 4 co7dzuup_xuczw9vc_AAACGCGCATTGTGTG-1 Naive
## 5 co7dzuup_xuczw9vc_AAAGCGGGTTTGGGCG-1 GC-Tfh-SAP
## 6 co7dzuup_xuczw9vc_AAATGCCTCACCTGTC-1 GC-Tfh-0X40
DimPlot(seurat,
group.by = "annotation_paper",
cols = color_palette,
pt.size = 0.1)
seurat_ATAC <- readRDS(path_to_obj)
seurat_ATAC
## An object of class Seurat
## 93602 features across 16383 samples within 1 assay
## Active assay: peaks_redefined (93602 features, 93293 variable features)
## 3 dimensional reductions calculated: umap, lsi, harmony
p1 <- DimPlot(seurat_ATAC,
pt.size = 0.1)
p1
Annotation level 5 for scATAC will be defined “a priori” as unannotated and the scRNA annotation will be transfered to the scATAC-multiome cells based on the same cell barcode.
tonsil_scATAC_df <- data.frame(cell_barcode = colnames(seurat_ATAC)[seurat_ATAC$assay == "scATAC"])
tonsil_scATAC_df$annotation_paper <- "unannotated"
df_all <- rbind(tonsil_RNA_annotation,tonsil_scATAC_df)
rownames(df_all) <- df_all$cell_barcode
df_all <- df_all[colnames(seurat_ATAC), ]
seurat_ATAC$annotation_paper <- df_all$annotation_paper
seurat_ATAC@meta.data$annotation_prob <- 1
melt(table(seurat_ATAC$annotation_paper))
## Var1 value
## 1 Naive 2551
## 2 CM Pre-non-Tfh 1233
## 3 CM PreTfh 276
## 4 T-Trans-Mem 233
## 5 T-Eff-Mem 306
## 6 T-helper 340
## 7 Tfh T:B border 26
## 8 Tfh-LZ-GC 1299
## 9 GC-Tfh-SAP 1176
## 10 GC-Tfh-0X40 238
## 11 Tfh-Mem 467
## 12 Eff-Tregs 477
## 13 non-GC-Tf-regs 155
## 14 GC-Tf-regs 240
## 15 unannotated 7366
table(is.na(seurat_ATAC$annotation_paper))
##
## FALSE
## 16383
DimPlot(seurat_ATAC,
group.by = "annotation_paper",
split.by = "assay",
cols = color_palette,
pt.size = 0.5)
Data splicing basically involves splitting the data set into training and testing data set.
reference_cells <- colnames(seurat_ATAC)[seurat_ATAC$assay == "multiome"]
query_cells <- colnames(seurat_ATAC)[seurat_ATAC$assay == "scATAC"]
reduction_ref <- seurat_ATAC@reductions[[reduction]]@cell.embeddings[reference_cells, dims]
reduction_query <- seurat_ATAC@reductions[[reduction]]@cell.embeddings[query_cells, dims]
We’re going to calculate the number of observations in the training dataset that correspond to the Multiome data. The reason we’re doing this is that we want to initialize the value of ‘K’ in the KNN model. To do that, we split our training data in two part: a train.loan, that correspond to the random selection of the 70% of the training data and the test.loan, that is the remaining 30% of the data set. The first one is used to traint the system while the second is uses to evaluate the learned system.
dat.d <- sample(1:nrow(reduction_ref),
size=nrow(reduction_ref)*0.7,replace = FALSE)
train.loan <- reduction_ref[dat.d,] # 70% training data
test.loan <- reduction_ref[-dat.d,] # remaining 30% test data
train.loan_labels <- seurat_ATAC@meta.data[row.names(train.loan),]$annotation_paper
test.loan_labels <- seurat_ATAC@meta.data[row.names(test.loan),]$annotation_paper
k.optm <- c()
k.values <- c()
for (i in c(2,4,8,10,12,14,16,32,64)){
print(i)
knn.mod <- knn(train=train.loan, test=test.loan, cl=train.loan_labels, k=i)
k.optm <- c(k.optm, 100 * sum(test.loan_labels == knn.mod)/NROW(test.loan_labels))
k.values <- c(k.values,i)
}
## [1] 2
## [1] 4
## [1] 8
## [1] 10
## [1] 12
## [1] 14
## [1] 16
## [1] 32
## [1] 64
Now we can plot the accuracy of the model taking in account a range of different K and selec the best one.
k.optim = data.frame(k.values,k.optm)
p3 <- ggplot(data=k.optim, aes(x=k.values, y=k.optm, group=1)) +
geom_line() +
geom_point() +
geom_vline(xintercept=10, linetype="dashed", color = "red")
p3
train.loan <- reduction_ref
test.loan <- reduction_query
train.loan_labels <- seurat_ATAC@meta.data[row.names(train.loan),]$annotation_paper
test.loan_labels <- seurat_ATAC@meta.data[row.names(test.loan),]$annotation_paper
knn.mod <- knn(train=train.loan, test=test.loan, cl=train.loan_labels, k=10, prob=T)
annotation_data <- data.frame(query_cells, knn.mod, attr(knn.mod,"prob"))
colnames(annotation_data) <- c("query_cells",
"annotation_paper",
"annotation_prob")
annotation_data$annotation_paper <- as.character(annotation_data$annotation_paper)
seurat_ATAC@meta.data[annotation_data$query_cells,]$annotation_paper <- annotation_data$annotation_paper
seurat_ATAC@meta.data[annotation_data$query_cells,]$annotation_prob <- annotation_data$annotation_prob
seurat_ATAC$annotation_paper <- factor(seurat_ATAC$annotation_paper)
DimPlot(
seurat_ATAC,
cols = color_palette,
group.by = "annotation_paper",
pt.size = 0.1) + ggtitle("")
#ggsave(path = "/Users/pauli/Desktop/",
# filename = "CD4_T_umap_level_5_ATAC.png",
# width = 20, height = 15, units = "cm")
DimPlot(
cols = color_palette,
seurat_ATAC, reduction = "umap",
group.by = "annotation_paper",
pt.size = 0.1, split.by = "assay")
saveRDS(seurat_ATAC, path_to_save)
general_counts_melt <- melt(table(seurat_ATAC$annotation_paper))
ggdotchart(general_counts_melt,
x = "Var1",
y = "value",
xlab = FALSE,
ylab = FALSE,
sorting = "none",
add = "segments",
color = 'gray80',
rotate = TRUE,
dot.size = 10,
label = round(general_counts_melt$value),
font.label = list(color = "black",
size = 9, vjust = 0.5),
ggtheme = theme_pubr()) +
theme(axis.text.x = element_text(angle = 90,
hjust = 1, size=9),
axis.text.y = element_text(size=9)) +
scale_y_continuous(limits=c(0, 5000))
Note that the probability of the prediction was lower in the transitioning cells and in not-defined clusters.
seurat_ATAC_scATAC = subset(seurat_ATAC, assay == "scATAC")
FeaturePlot(
seurat_ATAC_scATAC, reduction = "umap",
features = "annotation_prob",
pt.size = 0.1)
sessionInfo()
## R version 4.0.3 (2020-10-10)
## Platform: x86_64-apple-darwin13.4.0 (64-bit)
## Running under: macOS Big Sur 10.16
##
## Matrix products: default
## BLAS/LAPACK: /Users/pauli/opt/anaconda3/envs/Motif_TF/lib/libopenblasp-r0.3.10.dylib
##
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## attached base packages:
## [1] stats4 grid stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] ggpubr_0.4.0 reshape2_1.4.4 class_7.3-17 harmony_1.0 Rcpp_1.0.6 plyr_1.8.6 forcats_0.5.0 stringr_1.4.0 dplyr_1.0.7 purrr_0.3.4 readr_1.4.0 tidyr_1.1.3 tibble_3.1.2 ggplot2_3.3.5 tidyverse_1.3.0 flexclust_1.4-0 modeltools_0.2-23 lattice_0.20-41 Signac_1.2.1 SeuratObject_4.0.2 Seurat_4.0.3 BiocStyle_2.16.1
##
## loaded via a namespace (and not attached):
## [1] utf8_1.2.1 reticulate_1.20 tidyselect_1.1.1 htmlwidgets_1.5.3 docopt_0.7.1 BiocParallel_1.22.0 Rtsne_0.15 munsell_0.5.0 codetools_0.2-17 ica_1.0-2 future_1.21.0 miniUI_0.1.1.1 withr_2.4.2 colorspace_2.0-2 knitr_1.30 rstudioapi_0.11 ROCR_1.0-11 ggsignif_0.6.0 tensor_1.5 listenv_0.8.0 labeling_0.4.2 slam_0.1-47 GenomeInfoDbData_1.2.3 polyclip_1.10-0 farver_2.1.0 rprojroot_2.0.2 parallelly_1.26.1 vctrs_0.3.8 generics_0.1.0 xfun_0.18 lsa_0.73.2 ggseqlogo_0.1 R6_2.5.0 GenomeInfoDb_1.24.0 bitops_1.0-7 spatstat.utils_2.2-0 assertthat_0.2.1 promises_1.2.0.1 scales_1.1.1 gtable_0.3.0 globals_0.14.0 goftest_1.2-2 rlang_0.4.11 RcppRoll_0.3.0 splines_4.0.3 rstatix_0.6.0 lazyeval_0.2.2 spatstat.geom_2.2-0 broom_0.7.2 BiocManager_1.30.10 yaml_2.2.1
## [52] abind_1.4-5 modelr_0.1.8 backports_1.1.10 httpuv_1.6.1 tools_4.0.3 bookdown_0.21 ellipsis_0.3.2 spatstat.core_2.2-0 RColorBrewer_1.1-2 BiocGenerics_0.34.0 ggridges_0.5.3 zlibbioc_1.34.0 RCurl_1.98-1.2 rpart_4.1-15 deldir_0.2-10 pbapply_1.4-3 cowplot_1.1.1 S4Vectors_0.26.0 zoo_1.8-9 haven_2.3.1 ggrepel_0.9.1 cluster_2.1.0 here_1.0.1 fs_1.5.0 magrittr_2.0.1 data.table_1.14.0 scattermore_0.7 openxlsx_4.2.3 lmtest_0.9-38 reprex_0.3.0 RANN_2.6.1 SnowballC_0.7.0 fitdistrplus_1.1-5 matrixStats_0.59.0 hms_0.5.3 patchwork_1.1.1 mime_0.11 evaluate_0.14 xtable_1.8-4 rio_0.5.16 sparsesvd_0.2 readxl_1.3.1 IRanges_2.22.1 gridExtra_2.3 compiler_4.0.3 KernSmooth_2.23-17 crayon_1.4.1 htmltools_0.5.1.1 mgcv_1.8-33 later_1.2.0 lubridate_1.7.9
## [103] DBI_1.1.0 tweenr_1.0.1 dbplyr_1.4.4 MASS_7.3-53 Matrix_1.3-4 car_3.0-10 cli_3.0.0 parallel_4.0.3 igraph_1.2.6 GenomicRanges_1.40.0 pkgconfig_2.0.3 foreign_0.8-80 plotly_4.9.4.1 spatstat.sparse_2.0-0 xml2_1.3.2 XVector_0.28.0 rvest_0.3.6 digest_0.6.27 sctransform_0.3.2 RcppAnnoy_0.0.18 spatstat.data_2.1-0 Biostrings_2.56.0 rmarkdown_2.5 cellranger_1.1.0 leiden_0.3.8 fastmatch_1.1-0 uwot_0.1.10 curl_4.3.2 shiny_1.6.0 Rsamtools_2.4.0 lifecycle_1.0.0 nlme_3.1-150 jsonlite_1.7.2 carData_3.0-4 viridisLite_0.4.0 fansi_0.5.0 pillar_1.6.1 fastmap_1.1.0 httr_1.4.2 survival_3.2-7 glue_1.4.2 zip_2.1.1 qlcMatrix_0.9.7 png_0.1-7 ggforce_0.3.2 stringi_1.6.2 blob_1.2.1 irlba_2.3.3 future.apply_1.7.0